cMenu = {}
cMenu.Visible = false
cMenu.Width = ScrW()/3
cMenu.Height = ScrH()/2.25
cMenu.X	= 0
cMenu.Y = 0
cMenu.Title = "Gun Turret"
cMenu.ButtonInfo = {}
cMenu.Buttons = {}
cMenu.Children = {}
cMenu.FromTop = 0
cMenu.Font = "Gun"
cMenu.MaxButtonWidth = 0
cMenu.Plugins = {}
cMenu.ActivePlugin = nil
cMenu.Turret = nil

-- manual plugin include
include("menu_plugins/menu_stats.lua")
include("menu_plugins/menu_upgrade.lua")

function cMenu:Init()		
	surface.CreateFont("Very Damaged UL",72,400,true,false,"Gun")
	surface.CreateFont("Harbara",72,600,true,true,"Normal")
	surface.CreateFont("BIRTH OF A HERO", 36, 300, true, true, "CatHeader")
	surface.CreateFont("IglooLaser",72,100,true,false,"Ice")
	surface.SetFont(self.Font)
	
	self.Visible = false
	self.Children = {}
	self.closeButton = nil
	self.ButtonInfo = {}
	self.Buttons = {}
	self.Title = ""
	self.MaxButtonWidth = 0

	if self.FromTop <= 0 then
		self.FromTop = surface.GetTextSize("M")*2+16
	end	
end

function cMenu:Register(w,h,x,y)
	self.Width = w
	self.Height = h
	self.X = x
	self.Y = y
end

function cMenu:SetVisible(bool)
	if not type(bool) == "boolean" then return end
	self.Visible = bool
	for _, com in pairs(self.Children) do
		com:SetVisible(bool)
	end	
	gui.EnableScreenClicker( self.Visible )
end

function cMenu:SetTitle(title)
	self.Title = title
end

function cMenu:SetTurret(turret)
	self.Turret = turret
	self.Title = turret.Title
end

function cMenu:AddButton(mod,w,h,txt,icon)
	local x,y = self.X + 36, self.Y + (#self.ButtonInfo)*(h+12) + self.FromTop + 72
	local btn = {ID = #self.ButtonInfo, Module = mod, Width = w, Height = h, Text = txt or "Test", Icon = icon or "", X = x, Y = y}
	self.MaxButtonWidth = math.max(w+36, self.MaxButtonWidth)
	table.insert(self.ButtonInfo, btn)
end

function cMenu:DrawMenu()	
	-- draw the main rectangle
	surface.SetDrawColor( 0,0,0,235 )
	surface.DrawRect( 0, 0, self.Width, self.Height )
	
	-- draw the white outline
	surface.SetDrawColor( 255, 255, 255, 250 )
	surface.DrawOutlinedRect( 0, 0, self.Width, self.Height)
	
	-- create a close button
	if not self.closeButton then
		self.closeButton = vgui.Create("cCloseButton", self)
		self.closeButton:SetImage("gui/nonsense/close2")
		self.closeButton:SetPos(self.X+self.Width-16-9,self.Y+9)			
		self.closeButton:SetSize(16,16)
		self.closeButton:SetVisible(true)
		table.insert(self.Children, self.closeButton)
	end
				
	local xOffset = 0			
	-- draw the icon for the turret
	if self.Turret then
		surface.SetMaterial(Material("gui/nonsense/icons/"..self.Turret.Icon))
		surface.DrawTexturedRect(30, 20, 64, 64)
		xOffset = 64+36
	end
		
	-- create the title header
		surface.SetFont(self.Font)
		surface.SetTextColor( 255, 255, 255, 255 )
		surface.SetTextPos( 16 + xOffset, 16 )
		surface.DrawText(self.Title)
		
	-- draw the line below the title
		surface.SetDrawColor(255, 255, 255, 250)
		surface.DrawLine( 16, self.FromTop, self.Width-16, self.FromTop )	
	
end


function cMenu:DrawButtons()
	for key, button in pairs(self.ButtonInfo) do
		if not self.Buttons[key] then
			btn = vgui.Create("cButton", self)
			btn:SetSize(button.Width, button.Height)			
			btn:SetPos(button.X, button.Y)
			if button.Icon != "" then btn:SetImage(button.Icon) end
			btn:SetText(button.Text)
			btn:Register(button.Width, button.Height, button.X, button.Y)
			btn:SetVisible(true)
			btn.OnClick = function()
							for _, b in pairs(self.Buttons) do
								b:SetActive(false)
							end							
						  end
			btn.OnMouseDown = function()
							PrintTable(self.Plugins)
							if self.Plugins[button.Module] then
								self.Plugins[button.Module].cmd(self.Plugins[button.Module].arg1)
							else
								error("No Plugins loaded or invalid plugin set as button action.")
							end
						end
			self.Buttons[key] = btn
			table.insert(self.Children, btn)
		end
	end
end


function cMenu:AllButtons()
	return self.Buttons
end

function cMenu:Paint()	
	if (self.Visible == true) then
	
		self:DrawMenu()
		
		if #self.ButtonInfo > 0 then
			self:DrawButtons()
		end
		
		if cMenu.ActivePlugin then
			plugin = cMenu.ActivePlugin
			plugin.Paint(self, plugin)
		end
		
	end
	return true
end
		
vgui.Register("cMenu", cMenu, "Panel")